package milktea.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import milktea.dao.DBHelper;
import milktea.dao.IProductDao;
import milktea.mapper.impl.ProductMapper;
import milktea.model.Product;

public class ProductDao implements IProductDao {

	@Override
	public int insert(Product product) throws SQLException{
		DBHelper<Product> help = null;
		try {
			help = new DBHelper<Product>();
			String sql = "INSERT INTO CATAGORY VALUES(?,?,?,?,?,?,?)";
			int rows = help.executeUpdate(sql,String.valueOf(product.getProductId())
			,String.valueOf(product.getCatagoryId()),product.getProductName()
			,product.getProductDesc(),String.valueOf(product.getProductPrice())
			,product.getProductIcon(),String.valueOf(product.getProductStatus()));
			return rows;
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			help.close();
		}
		return 0;
	}

	@Override
	public List<Product> select() throws SQLException {
		DBHelper<Product> help = null;
		try {
			List<Product> list = new ArrayList<Product>();
			help = new DBHelper<Product>();
			String sql = "SELECT * FROM PRODUCT";
			list = help.executeQuery(sql, new ProductMapper());
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			help.close();
		}
		return null;
	}

	@Override
	public List<Product> selectByCataId(Integer catagoryId) {
		DBHelper<Product> help = null;
		try {
			List<Product> list = new ArrayList<Product>();
			help = new DBHelper<Product>();
			String sql = "SELECT * FROM PRODUCT WHERE CATAGORY_ID = ?";
			list = help.executeQuery(sql, new ProductMapper(),catagoryId);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			help.close();
		}
		return null;
	}

}
